/*
名称:访问服务器组件
作者:唐成
日期:2019-04-24
版本:1.0
使用 : 请参见文件底部:调用格式说明
*/
var zcServer = {
  ServerUrl: "http://127.0.0.1:8056/",
  IsDebug: true,

  IsNeedLogin: "true",
  setCfg: function(options) {
    let me = this;
    me.ServerUrl = options.Scope.$ZcPublic.Server;
    if (me.ServerUrl.indexOf("http://127.0.0.1") >= 0) {
      me.IsDebug = true;
    }
    else {
      me.IsDebug = false;
    }
  },
  Get: function(options) {
    let me = this;
    me.setCfg(options);
    if(me.IsDebug) {
      me.getByJsonP(options);
    }
    else {
      me.getByNormal(options);
    }
  },
  Post: function(options) {
    let me = this;
    me.setCfg(options);
    if(me.IsDebug) {
      me.postByJsonP(options);
    }
    else {
      me.postByNormal(options);
    }
  },
  PostOss: function(options) {
    let me = this;
    me.setCfg(options);
    if(me.IsDebug) {
      me.postOssByJsonP(options);
    }
    else {
      me.postOssByNormal(options);
    }
  },
  DownOss: function(options) {
    let me = this;
    me.setCfg(options);
    window.location.href = me.ServerUrl + 'PageService/doOSSDownload.ashx?pSN=' + options.FileSN;
  },
  getByNormal: function(options) {
    let me = this;
    me.setCfg(options);
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'VueService/doNewResourceLocal.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();

    options.Scope.$ZcPublic.Http.post(curServiceUrl,options.Paras)
    .then(function(response) {
      if(response.status === 200) {
        if(options.Callback) {
          options.Callback(response.data,options.Scope);
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err) {
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  getByJsonP: function(options) {
    let me = this;
    me.setCfg(options);
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    if(options.ServerUrl){
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    }
    else{
      curServiceUrl = me.ServerUrl + 'VueService/doNewResourceDebug.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();
    }
    options.Scope.$ZcPublic.Http.post(curServiceUrl,JSON.stringify(options.Paras),{withCredentials:true})
    .then(function(response) {
      if(response.status === 200) {
        if(options.Callback) {
          options.Callback(response.data,options.Scope);
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err) {
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  postByNormal: function(options) {
    let me = this;
    me.setCfg(options);
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'VueService/doNewResourceLocal.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();

    options.Scope.$ZcPublic.Http.post(curServiceUrl,options.Paras)
    .then(function(response) {
      if(response.status === 200) {
        if(options.Callback) {
          options.Callback(response.data,options.Scope);
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err) {
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  postByJsonP: function(options) {
    let me = this;
    me.setCfg(options);
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    // console.log('me.IsNeedLogin', me.IsNeedLogin);
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'VueService/doNewResourceDebug.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();

    options.Scope.$ZcPublic.Http.post(curServiceUrl,JSON.stringify(options.Paras),{withCredentials:true})
    .then(function(response) {
      //console.log('response999', response);
      if(response.status === 200) {
        if(options.Callback) {
          options.Callback(response.data,options.Scope);
        }
      }
      else {
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err) {
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  postOssByNormal: function(options) {
    let me = this;
    me.setCfg(options);
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'PageService/doOSSService.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();
    options.Source.Scope.$ZcPublic.Http.post(curServiceUrl,options.Paras)
    .then(function(response) {
      if(response.status === 200) {
        if(options.Callback) {
          options.Callback(response.data, {
            UserData:options.Source.UserData
          });
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err) {
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  postOssByJsonP: function (options) {
    let me = this;
    me.setCfg(options);
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = "";

    if(options.ServerUrl){
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    }
    else{
      curServiceUrl = me.ServerUrl + 'VueService/doVueOSSDebugService.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();
    }
    options.Source.Scope.$ZcPublic.Http.post(curServiceUrl,JSON.stringify(options.Paras),{withCredentials:true})
    .then(function(response) {
      if(response.status === 200) {
        if(options.Callback) {
          options.Callback(response.data, {
            UserData:options.Source.UserData
          });
        }
      }
      else {
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        }, options.Scope);
      }
    })
    .catch(function(err) {
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      }, options.Scope);
    });
  },
  getDc: function () {
    return new Date().getTime();
  }
}

export   {zcServer};
/*
调用格式:
<script>
me.$ZcPublic.Server.Post({
    IsNeedLogin: 'false',//根据需求而定
    ServerUrl: 'VueService/doVueLogin.ashx',//根据需求而定
    Paras: {
        Name: 'VueTest',
        Action: 'getOpenId',
        UserId: 'A001',//根据需求而定
        Data: {
          Id: 5
        }
    },
    Callback: me.Callback_GetOpenId,
    Scope: me
});
</script>
 */
